import { ApiCall } from "tsrpc";
import { ReqSendMail, ResSendMail } from "../../../../shared/master/body/mail/PtlSendMail";
import { player } from "../../../../model/fight/body/player";
import { template } from "../../../../shared/master/MsgAction";
import { face_prop_item } from "../../../../shared/interface";
import god from "../../../../god";
import app from "../../../../app";

export default async function (call: ApiCall<ReqSendMail, ResSendMail>) {
    call.succ({})
    let user = call.req._player as player;
    let title = call.req.title;
    if(!title){
        user.sendMsg(template.default,'✉️发件失败:邮件标题不能为空')
        return;
    }
    let content = call.req.content;
    if(!content){
        user.sendMsg(template.default,'✉️发件失败:邮件内容不能为空')
        return;
    }
    let to_uuid = call.req.to_uuid;
    if(!to_uuid){
        user.sendMsg(template.default,`✉️发件失败:收件人不能为空`)
        return;
    }
    let bag_idx = call.req.bag_idx;
    let bag = user.getBag();
    const cfg = god.getServerCfgItem('mail_gas')
    let log = user.check_wallet(Number(cfg.a) * bag_idx.length, 0, [], false, '✉️邮件发送手续费', true);
    if(!log.state){
        return;
    }
    let annexs:face_prop_item[] = [];
    for (let i = 0; i < bag_idx.length; i++) {
        const element = bag_idx[i];
        const bat_item = bag.items[element - 1];
        if(!bat_item){
            user.sendMsg(template.default,`✉️发件失败:附件ID${element},未找背包找到对应ID`)
            return;
        }
        annexs.push(app.sanitizeObject(bat_item))
        bat_item.num = 0;
    }
    await god.send({uuid:user.uuid,name:user.name},to_uuid,title,content,annexs);


    user.sendMsg(template.default,`✉️发件成功🎉`)
}